package seller

import (
    "time"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//店铺分账分组表  
type FzGroups struct {
	FzGroupId   int64     `orm:"column(fz_group_id);pk;auto" json:"fz_group_id"` 
	Name        string    `orm:"column(name)" json:"name"`                  
	PartnerId   int64     `orm:"column(partner_id);unique" json:"partner_id"`
	Remark      string    `orm:"column(remark)" json:"remark"`          
	CreatedTime time.Time `orm:"column(created_time);type(datetime)" json:"created_time"`
	UpdateTime  time.Time `orm:"column(update_time);type(datetime)" json:"update_time"`
}

//店铺分账表
type FzStores struct {
	FsId        int64     `orm:"column(fs_id);pk;auto" json:"fs_id"` 
	StoreId     int64     `orm:"column(store_id);unique" json:"store_id"` 
	FzGroupId   int64     `orm:"column(fz_group_id)" json:"fz_group_id"`
	PartnerId   int64     `orm:"column(partner_id)" json:"partner_id"` //
	Z1          string    `orm:"column(z1)" json:"z1"`             // 
	Z1name      string    `orm:"column(z1name)" json:"z1name"`     // 
	Z2          string    `orm:"column(z2)" json:"z2"`             // 
	Z2name      string    `orm:"column(z2name)" json:"z2name"`     // 
	Z3          string    `orm:"column(z3)" json:"z3"`             // 
	Z3name      string    `orm:"column(z3name)" json:"z3name"`     // 
	Z4          string    `orm:"column(z4)" json:"z4"`             // 
	Z4name      string    `orm:"column(z4name)" json:"z4name"`     // 
	Z5          string    `orm:"column(z5)" json:"z5"`             // 
	Z5name      string    `orm:"column(z5name)" json:"z5name"`     // 
	CreatedTime time.Time `orm:"column(created_time);type(datetime)" json:"created_time"`
	UpdateTime  time.Time `orm:"column(update_time);type(datetime)" json:"update_time"`
}

//分账店铺商品表  
type FzItems struct {
	FiId        int64     `orm:"column(fi_id);pk;auto" json:"fi_id"` 
	Barcode     string    `orm:"column(barcode)" json:"barcode"`        //商品备案编码            
	PartnerId   int64     `orm:"column(partner_id)" json:"partner_id"`  
	FzGroupId   int64     `orm:"column(fz_group_id)" json:"fz_group_id"` 
	R1          float32   `orm:"column(r1)" json:"r1"`     //z1账号分账比例
	R2          float32   `orm:"column(r2)" json:"r2"`     //z2账号分账比例
	R3          float32   `orm:"column(r3)" json:"r3"`     //
	R4          float32   `orm:"column(r4)" json:"r4"`     //
	R5          float32   `orm:"column(r5)" json:"r5"`     //
	CreatedTime time.Time `orm:"column(created_time);type(datetime)" json:"created_time"`
	UpdateTime  time.Time `orm:"column(update_time);type(datetime)" json:"update_time"`
}

//店铺分账日志表
type FzLogs struct {
	FzLogId     int64     `orm:"column(fz_log_id);pk;auto" json:"fz_log_id"` 
	StoreId     int64     `orm:"column(store_id);unique" json:"store_id"` 
	PartnerId   int64     `orm:"column(partner_id)" json:"partner_id"` //
	OrderId     int64     `orm:"column(order_id)" json:"order_id"` //
	Z0          string    `orm:"column(z0)" json:"z0"`             // 
	Z0money     float32   `orm:"column(z0money)" json:"z0money"`   //主帐号分账钱数
	Z1          string    `orm:"column(z1)" json:"z1"`             // 
	Z1money     float32   `orm:"column(z1money)" json:"z1money"`     // 
	Z2          string    `orm:"column(z2)" json:"z2"`             // 
	Z2money     float32   `orm:"column(z2money)" json:"z2money"`     // 
	Z3          string    `orm:"column(z3)" json:"z3"`             // 
	Z3money     float32   `orm:"column(z3money)" json:"z3money"`     // 
	Z4          string    `orm:"column(z4)" json:"z4"`             // 
	Z4money     float32   `orm:"column(z4money)" json:"z4money"`     // 
	Z5          string    `orm:"column(z5)" json:"z5"`             // 
	Z5money     float32   `orm:"column(z5money)" json:"z5money"`     // 
	Total       float32   `orm:"column(total)" json:"total"`     // 
	State       int       `orm:"column(state)" json:"state"`     //状态默认是1=已提交银联 2=银联支付成功 
	CreatedTime time.Time `orm:"column(created_time);type(datetime)" json:"created_time"`
	UpdateTime  time.Time `orm:"column(update_time);type(datetime)" json:"update_time"`
}

func init() {
    orm.RegisterModel(new(FzGroups),new(FzStores),new(FzItems),new(FzLogs))   
}

func GetFzGroups(fzGroupId int64) (fzGroups *FzGroups, err error) {
	fzGroups = &FzGroups{}
	o := models.GetDefaultDB()
	err = o.QueryTable(new(FzGroups)).Filter("fz_group_id", fzGroupId).One(fzGroups)
	return
}

func GetFzStore(storeId int64) (fzStores *FzStores, err error) {
	fzStores = &FzStores{}
	o := models.GetDefaultDB()
	err = o.QueryTable(new(FzStores)).Filter("store_id", storeId).One(fzStores)
	return
}

func GetFzItem(fzGroupId int64, barcode string) (fzItems *FzItems, err error) {
	fzItems = &FzItems{}
	o := models.GetDefaultDB()					
	err = o.QueryTable(new(FzItems)).Filter("fz_group_id",fzGroupId).Filter("barcode",barcode).One(fzItems)
	return
}

func AddFzLog(m *FzLogs) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}


